package com.barnaby.sudoku;

import java.util.LinkedHashSet;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.*;
import android.view.*;
import android.widget.*;

public class GameActivity extends Activity {
	private static final String TAG = "Sudoku";
	
	public static final String KEY_DIFFICULTY = "com.barnaby.sudoku.difficulty";
	public static final int DIFFICULTY_EASY = 0;
	public static final int DIFFICULTY_MEDIUM = 1;
	public static final int DIFFICULTY_HARD = 2;
	//private int puzzle[];
	private BoardView puzzleView;
	private IBoard board;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "onCreate");
		int diff = getIntent().getIntExtra(KEY_DIFFICULTY,
		DIFFICULTY_EASY);
		//puzzle = getPuzzle(diff);
		board = Board.MakeNewBoard();
		board.TrySetBoardCellValues(getPuzzle(diff));
		//calculateUsedTiles();
		puzzleView = new BoardView(this);
		setContentView(puzzleView);
		puzzleView.requestFocus();
	}
	
	private final String easyPuzzle =
	"360000000004230800000004200" +
	"070460003820000014500013020" +
	"001900000007048300000000045";
	private final String mediumPuzzle =
	"650000070000506000014000005" +
	"007009000002314700000700800" +
	"500000630000201000030000097";
	private final String hardPuzzle =
	"009000000080605020501078000" +
	"000000700706040102004000000" +
	"000720903090301080000000600";
	
	//private int[] getPuzzle(int diff) {
	private String getPuzzle(int diff) {
		String puz;
		// TODO: Continue last game
		switch (diff) {
			case DIFFICULTY_HARD:
			puz = hardPuzzle;
			break;
			case DIFFICULTY_MEDIUM:
			puz = mediumPuzzle;
			break;
			case DIFFICULTY_EASY:
			default:
			puz = easyPuzzle;
			break;
		}
		//return fromPuzzleString(puz);
		return puz;
	}
	
//	static private String toPuzzleString(int[] puz) {
//		StringBuilder buf = new StringBuilder();
//		for (int element : puz) {
//			buf.append(element);
//		}
//		return buf.toString();
//	}
	
	public int getNumberOfPossibleValues(int column, int row){
		return board.GetPossibleCellValues(column, row).size();
	}
	
	static protected int[] fromPuzzleString(String string) {
		int[] puz = new int[string.length()];
		for (int i = 0; i < puz.length; i++) {
			puz[i] = string.charAt(i) - '0';
		}
		return puz;
	}
	
//	private int getTile(int column, int row) {
//		//return puzzle[y * 9 + x];
//		return board.GetCellValue(column, row);
//	}
	
	public boolean setTile(int column, int row, int value) {
		//puzzle[y * 9 + x] = value;
		return board.TrySetCellValue(column, row, value);
	}
		
	protected String getTileString(int column, int row) {
		int value = board.GetCellValue(column, row);
		return value == 0 ? "" : String.valueOf(value);		
	}
	
	protected void showKeypadOrError(int x, int y) {
		//int tiles[] = getUsedTiles(x, y);
		LinkedHashSet<Integer> tiles =  board.GetPossibleCellValues(x, y);
		if (tiles.size() == 1) {
			Toast toast = Toast.makeText(this,R.string.no_moves_label, Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		} else {
			//Log.d(TAG, "showKeypad: used=" + toPuzzleString(tiles));
			Dialog v = new Keypad(this, tiles, puzzleView);
			v.show();
		}
	}
		
	
}
